package com.company.beauty.ids.support;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.company.beauty.ids.handler.SnowflakeHandler;
import com.company.beauty.ids.handler.SnowflakeHandlerFactory;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * <p>Ids 工具类</p >
 *
 * @author youyoukele
 * @version 1.0
 * @date 2023/8/11 11:28
 */
@Slf4j
public class IdsUtils {

    private final static SnowflakeHandler SNOWFLAKE_HANDLER = SnowflakeHandlerFactory.getSnowflakeHandler();

    private final static Snowflake SNOWFLAKE = IdUtil.getSnowflake(SNOWFLAKE_HANDLER.getWorkerId(), SNOWFLAKE_HANDLER.getCenterId());

    /**
     * 雪花编号（单个）
     *
     * @return
     */
    public static Long snowId() {
        return SNOWFLAKE.nextId();
    }

    /**
     * 雪花编号（批量）
     *
     * @param length
     * @return
     */
    public static List<Long> snowIds(Integer length) {
        return IntStream.range(0, length)
                .mapToObj(it -> SNOWFLAKE.nextId())
                .collect(Collectors.toList());
    }

}
